package dao;

import user.Manager;
import util.DbUtil;
import util.DruidUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ManagerDaoImp implements ManagerDao{

    @Override
    public Manager managerLogin(Manager manager) {
        Connection con = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            //1、获取数据库连接
            con = DruidUtils.getConnection();
            //2、准备sql语句
            String sql = "select * from homework.manager where account = ? and password = ?";
            //3、执行后获取结果并返回
            preparedStatement = con.prepareStatement(sql);
            preparedStatement.setString(1, manager.getAccount());
            preparedStatement.setString(2,manager.getPassword());
            resultSet = preparedStatement.executeQuery();
            if(resultSet.next()){
                Manager manager1 = new Manager(resultSet.getString("account"),resultSet.getString("password"));
                return manager1;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            try {
                DruidUtils.close(con, preparedStatement);
                if(null != resultSet){
                    resultSet.close();
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
       return null;
    }
}
